package com.Yukiha.gachamod.gachaMenu.client.gui;

import com.Yukiha.gachamod.Data.ModelEntry;
import com.Yukiha.gachamod.gachaMenu.client.ClientGachaDataCache;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

import java.util.*;

@SuppressWarnings("removal")
public class GachaBookScreen extends AbstractContainerScreen<GachaBookMenu> {

    private static final ResourceLocation BACKGROUND = new ResourceLocation("gachamod", "textures/gui/gacha_book.png");
    private static final int ENTRIES_PER_PAGE = 6;

    private final List<ModelEntry> modelList = new ArrayList<>();

    public GachaBookScreen(GachaBookMenu menu, Inventory inv, Component title) {
        super(menu, inv, title);
        this.imageWidth = 176;
        this.imageHeight = 166;
        initModelList();
    }

    /** 初始化客户端缓存模型数据 */
    private void initModelList() {
        Map<String, Set<String>> drawnModels = ClientGachaDataCache.getAllDrawnModels();

        for (Map.Entry<String, Set<String>> entry : drawnModels.entrySet()) {
            String rarity = entry.getKey();
            for (String model : entry.getValue()) {
                modelList.add(new ModelEntry(model, rarity, "模型 [" + model + "] 的描述"));
            }
        }

        modelList.sort(Comparator.comparing(e -> e.modelName));
    }

    /** 背景渲染 + 模型条目渲染 + 翻页渲染 */
    @Override
    protected void renderBg(GuiGraphics guiGraphics, float partialTicks, int mouseX, int mouseY) {
        guiGraphics.blit(BACKGROUND, leftPos, topPos, 0, 0, imageWidth, imageHeight);

        int start = menu.getPage() * ENTRIES_PER_PAGE;
        int end = Math.min(start + ENTRIES_PER_PAGE, modelList.size());

        for (int i = start; i < end; i++) {
            ModelEntry entry = modelList.get(i);
            int y = topPos + 20 + (i - start) * 18;
            int color = switch (entry.rarity.toLowerCase()) {
                case "史诗" -> 0xAA00FF;
                case "优良" -> 0x55FFFF;
                case "普通" -> 0xCCCCCC;
                default -> 0xFFFFFF;
            };

            guiGraphics.drawString(this.font, Component.literal("[" + entry.rarity + "] ").append(entry.modelName), leftPos + 10, y, color);
        }

        drawPagination(guiGraphics);
    }

    /** 渲染分页按钮和页码 */
    private void drawPagination(GuiGraphics guiGraphics) {
        int totalPages = Math.max(1, (modelList.size() + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE);
        int y = topPos + imageHeight - 20;

        // 当前页数
        String pageInfo = "第 " + (menu.getPage() + 1) + " / " + totalPages + " 页";
        guiGraphics.drawCenteredString(this.font, pageInfo, leftPos + imageWidth / 2, y, 0x00FFFF);

        // 按钮提示文字
        guiGraphics.drawString(this.font, "<< 上一页", leftPos + 10, y, 0xFFFFFF);
        guiGraphics.drawString(this.font, "下一页 >>", leftPos + 110, y, 0xFFFFFF);
    }

    /** 处理鼠标点击翻页按钮 */
    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        int buttonY = topPos + imageHeight - 20;

        if (mouseY >= buttonY && mouseY <= buttonY + 10) {
            if (mouseX >= leftPos + 10 && mouseX <= leftPos + 60) {
                menu.previousPage();
                return true;
            } else if (mouseX >= leftPos + 110 && mouseX <= leftPos + 160) {
                menu.nextPage();
                return true;
            }
        }
        return super.mouseClicked(mouseX, mouseY, button);
    }

    /** 标题渲染 */
    @Override
    protected void renderLabels(GuiGraphics guiGraphics, int mouseX, int mouseY) {
        guiGraphics.drawString(this.font, this.title, 10, 10, 0x404040, false);
    }
}
